package com.htthorizon.powersupply.async;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.htthorizon.powersupply.domain.*;
import com.htthorizon.powersupply.dto.LineLoad;
import com.htthorizon.powersupply.service.*;
import com.htthorizon.powersupply.utils.ApiUtil;
import com.htthorizon.powersupply.utils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Brilliant
 */
@Component
@Slf4j
public class AsyncData {

    @Autowired
    private ApiUtil apiUtil;

    @Autowired
    private RealTimePowerOutageService realTimePowerOutageService;

    @Autowired
    private RealTimePowerOutageUserService realTimePowerOutageUserService;

    @Autowired
    private RdsGridTgPowerDfService rdsGridTgPowerDfService;
    @Autowired
    private RdsGridTgVoltDfService rdsGridTgVoltDfService;
    @Autowired
    private RdsGridTgCurDfService rdsGridTgCurDfService;
    @Autowired
    private AdsCstCjbdConsVoltloweDiService adsCstCjbdConsVoltloweDiService;
    @Autowired
    private LineLoadMonitorService lineLoadMonitorService;


    //
    @Scheduled(cron = "0 0/15 * * * ?")
    public void getLineLoad() throws URISyntaxException, IOException {
        List<LineLoad> lineLoad = apiUtil.getLineLoad("1", "2000");
        lineLoadMonitorService.compute(lineLoad);
    }

    @Scheduled(cron = "0 0/10 * * * ?")
    public void getUserLowVoltageData() throws URISyntaxException, IOException {
        Integer total = apiUtil.getAdsCstCjbdConsVoltloweDiTotalNum("1", "10");
        log.info("用户低电压数据总数:{}", total);
        if (total > 0) {
            String dateDt = DatePattern.PURE_DATE_FORMAT.format(new Date());
            LambdaQueryWrapper<AdsCstCjbdConsVoltloweDi> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AdsCstCjbdConsVoltloweDi::getDataDt, dateDt);
            adsCstCjbdConsVoltloweDiService.remove(queryWrapper);

            PageUtil<AdsCstCjbdConsVoltloweDi, Integer> realTimePowerOutageIntegerPageUtil = new PageUtil<>(total, 1000);
            List<PageUtil.Page<AdsCstCjbdConsVoltloweDi, Integer>> pages = realTimePowerOutageIntegerPageUtil.buildPage();
            for (PageUtil.Page<AdsCstCjbdConsVoltloweDi, Integer> page : pages) {
                List<AdsCstCjbdConsVoltloweDi> realTimePowerOutage = apiUtil.getAdsCstCjbdConsVoltloweDi(page.getPageNum().toString(), page.getPageSize().toString());
                for (AdsCstCjbdConsVoltloweDi adsCstCjbdConsVoltloweDi : realTimePowerOutage) {
                    adsCstCjbdConsVoltloweDi.setDataDt(dateDt);
                }
                adsCstCjbdConsVoltloweDiService.saveBatch(realTimePowerOutage);
            }
        }
    }



    @Scheduled(cron = "0 0/15 * * * ?")
    public void getGridTgPowerDf() throws URISyntaxException, IOException {
        Integer total = apiUtil.getGridTgPowerDfTotalNum("1", "10");
        log.info("获取功率总数:{}", total);
        if (total > 0) {
            String dateDt = DatePattern.PURE_DATE_FORMAT.format(new Date());
            LambdaQueryWrapper<RdsGridTgPowerDf> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(RdsGridTgPowerDf::getDataDt, dateDt);
            rdsGridTgPowerDfService.remove(queryWrapper);

            PageUtil<RdsGridTgPowerDf, Integer> realTimePowerOutageIntegerPageUtil = new PageUtil<>(total, 1000);
            List<PageUtil.Page<RdsGridTgPowerDf, Integer>> pages = realTimePowerOutageIntegerPageUtil.buildPage();
            for (PageUtil.Page<RdsGridTgPowerDf, Integer> page : pages) {
                List<RdsGridTgPowerDf> realTimePowerOutage = apiUtil.getGridTgPowerDf(page.getPageNum().toString(), page.getPageSize().toString());
                rdsGridTgPowerDfService.saveBatch(realTimePowerOutage);
            }
            rdsGridTgPowerDfService.compute();
        }
    }

//    @Scheduled(cron = "0 5/15 * * * ?")
    public void getGridTgVoltDf() throws URISyntaxException, IOException {
        Integer total = apiUtil.getGridTgVoltDfTotalNum("1", "10");
        log.info("电压:{}", total);
        if (total > 0) {
            String dateDt = DatePattern.PURE_DATE_FORMAT.format(new Date());
            LambdaQueryWrapper<RdsGridTgVoltDf> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(RdsGridTgVoltDf::getDataDt, dateDt);
            rdsGridTgVoltDfService.remove(queryWrapper);
            PageUtil<RdsGridTgVoltDf, Integer> realTimePowerOutageIntegerPageUtil = new PageUtil<>(total, 2000);
            List<PageUtil.Page<RdsGridTgVoltDf, Integer>> pages = realTimePowerOutageIntegerPageUtil.buildPage();
            for (PageUtil.Page<RdsGridTgVoltDf, Integer> page : pages) {
                List<RdsGridTgVoltDf> rdsGridTgVoltDfs = apiUtil.getGridTgVoltDf(page.getPageNum().toString(), page.getPageSize().toString());
                List<RdsGridTgVoltDf> collect = rdsGridTgVoltDfs.stream().filter(rdsGridTgVoltDf -> dateDt.equals(rdsGridTgVoltDf.getDataDt())).collect(Collectors.toList());
                rdsGridTgVoltDfService.saveBatch(collect);
            }
            rdsGridTgVoltDfService.compute();
        }

    }

//    @Scheduled(cron = "0 10/15 * * * ?")
    public void getGridTgCurDf() throws URISyntaxException, IOException {
        Integer total = apiUtil.getGridTgCurDfTotalNum("1", "10");
        if (total > 0) {
            log.info("电流:{}", total);
            String dateDt = DatePattern.PURE_DATE_FORMAT.format(new Date());
            LambdaQueryWrapper<RdsGridTgCurDf> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(RdsGridTgCurDf::getDataDt, dateDt);
            rdsGridTgCurDfService.remove(queryWrapper);
            PageUtil<RdsGridTgCurDf, Integer> realTimePowerOutageIntegerPageUtil = new PageUtil<>(total, 2000);
            List<PageUtil.Page<RdsGridTgCurDf, Integer>> pages = realTimePowerOutageIntegerPageUtil.buildPage();
            for (PageUtil.Page<RdsGridTgCurDf, Integer> page : pages) {
                List<RdsGridTgCurDf> rdsGridTgCurDfs = apiUtil.getGridTgCurDf(page.getPageNum().toString(), page.getPageSize().toString());
                List<RdsGridTgCurDf> collect = rdsGridTgCurDfs.stream().filter(rdsGridTgCurDf -> dateDt.equals(rdsGridTgCurDf.getDataDt())).collect(Collectors.toList());
                rdsGridTgCurDfService.saveBatch(collect);
            }
        }
    }

    @Scheduled(cron = "0 0/10 * * * ?")
    public void asyncDataRealTimePowerOutage() throws URISyntaxException, IOException {
        log.info("台区实时停电");
        Integer total = apiUtil.getRealTimePowerOutageTotalNum();
        if (total > 0) {
            String dateDt = DatePattern.PURE_DATE_FORMAT.format(new Date());
            LambdaQueryWrapper<RealTimePowerOutage> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(RealTimePowerOutage::getDataDt, dateDt);
            realTimePowerOutageService.remove(queryWrapper);

            log.info("台区实时停电，数量:{}", total);
            PageUtil<RealTimePowerOutage, Integer> realTimePowerOutageIntegerPageUtil = new PageUtil<>(total, 2000);
            List<PageUtil.Page<RealTimePowerOutage, Integer>> pages = realTimePowerOutageIntegerPageUtil.buildPage();
            for (PageUtil.Page<RealTimePowerOutage, Integer> page : pages) {
                List<RealTimePowerOutage> realTimePowerOutage = apiUtil.getRealTimePowerOutage(page.getPageNum().toString(), page.getPageSize().toString());
                realTimePowerOutageService.insertIngoreBatch(this.dataFilte(realTimePowerOutage));
            }
        }

    }

    @Scheduled(cron = "0 10/20 * * * ?")
    public void asyncDataRealTimePowerOutageUser() throws URISyntaxException, IOException {
        log.info("用户实时停电");
        String dateDt = DatePattern.PURE_DATE_FORMAT.format(new Date());
        LambdaQueryWrapper<RealTimePowerOutage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RealTimePowerOutage::getDataDt, dateDt);
        List<RealTimePowerOutage> list = realTimePowerOutageService.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            for (RealTimePowerOutage realTimePowerOutage : list) {
                Integer total = apiUtil.getRealTimePowerOutageUserTotalNum(realTimePowerOutage.getTgNo());
                if (total > 0) {
                    LambdaQueryWrapper<RealTimePowerOutageUser> queryWrapper2 = new LambdaQueryWrapper<>();
                    queryWrapper2.eq(RealTimePowerOutageUser::getDataDt, dateDt);
                    queryWrapper2.eq(RealTimePowerOutageUser::getTgNo, realTimePowerOutage.getTgNo());
                    realTimePowerOutageUserService.remove(queryWrapper2);
//                    log.info("用户实时停电,数量:{}", total);
                    List<PageUtil.Page<Object, Integer>> pages = new PageUtil<>(total, 2000).buildPage();
                    for (PageUtil.Page<Object, Integer> page : pages) {
                        List<RealTimePowerOutageUser> realTimePowerOutageUser = apiUtil.getRealTimePowerOutageUser(page.getPageNum().toString(), page.getPageSize().toString(), realTimePowerOutage.getTgNo());
                        realTimePowerOutageUserService.insertIngoreBatch(this.dataFilte2(realTimePowerOutageUser));
                    }
                }
            }
        }


    }

    private List<RealTimePowerOutage> dataFilte(List<RealTimePowerOutage> realTimePowerOutages) {
        String dataDt = DateUtil.format(DateUtil.date(), "yyyyMMdd");
        for (RealTimePowerOutage outage : realTimePowerOutages) {
            outage.setDataDt(dataDt);
        }
        return realTimePowerOutages;
    }


    private List<RealTimePowerOutageUser> dataFilte2(List<RealTimePowerOutageUser> realTimePowerOutageUser) {
        String dataDt = DateUtil.format(DateUtil.date(), "yyyyMMdd");
        for (RealTimePowerOutageUser user : realTimePowerOutageUser) {
            user.setDataDt(dataDt);
        }
        return realTimePowerOutageUser;
    }
}
